qx.Class.define("lastfmbackup.test.model.Compression", {
  
  extend : qx.dev.unit.TestCase,

  members : {
    
    testee : null,
  
    
    setUp : function()
    {
      this.testee = new lastfmbackup.model.Compression();
    },
    
    tearDown : function()
    {
      this.testee.dispose();
    },
    
    testCompress : function()
    {
      var count   = 0;
      var attempt = (function(callback)
      {
        var expectedComplete = [
          "DcoxDgIhEADArxBqNoHd5WCfYGFj7IzFwoGSXE4LjS/xXX5JuinmEigKeomMzh5e91HVYDK/rxHr7PFx++iYOLfd" +
            "nPQ51uleIy6aGXwOBZglQImSgatQq4GL73G22hp1WQJwkAScqYCSrpAIGVVSK6zW7e9tu/4B",
          "Fcw5DgIxDEDRu6QeS3ZsZynhCpSIwhkngMQiRXN/EbpfPP0rsVbiGCNu4TSP57D9CFs42+v9nY9uqy/HtM+9z5VE" +
            "I8fGHShWA/GcwZgLeCrN14Tc658p5sapQE8JQXZSsFEFeGhC1kQoupgkc/LKgFIjiA6H2qTAzkVdsaWIFG4/",
          "DcoxDsMgDEbhuzDHEgYbw5wjdKw6/MFQZUmGVj1/md4bvidnbRxbLWkLO64fPmELj3tcK/v9vs7v+RvrJcJ1QEg8" +
            "gsSKEiYiaR+tJ3iXzosZJE2LTtYjkxRPdHitZLm2YiiZZ1xsSoOYOiXWSTJLJigr5aNn4EhaTcLrDw==",
          "JYxNC8IwEET/Ssi5C0mz+bp7EsVLb+Ih2W40hVao1N/vgseZ92bu1vmUffbWDXp6sTrtXNa+PfWgz8f+Xvqm/+DS" +
            "v1KrK5fPsbO6NTX1lQXK3phACWqoI2AwBKWMBJVmTAmzM7GJJpGRGkJq1gGy91BTiECZmUuN0Vgvmgs2UCwMro" +
            "wRECmAfGQwzDO2bKmx0Y8f"
        ];
        this.testee.addListener("complete", function(event)
        {
          event.getData().forEach(function(item)
          {
            this.assertEquals("compress", item.method);
            this.assertEquals(expectedComplete.shift(), item.data);
          }, this);
          qx.event.Registration.removeAllListeners(this.testee);
          callback.call(this);
        }, this);
        
        var expectedProgress = Array.range(5).map(function(i)
        {
          return i / 4;
        });
        this.testee.addListener("changeProgress", function(event)
        {
          this.assertEquals(expectedProgress.shift(), event.getData());
        }, this);
        
        var data = lastfmbackup.test.mock.RecentTracksModel.data;
        this.testee
          .compress(qx.lang.Json.stringify(data[0]))
          .compress(qx.lang.Json.stringify(data[1]))
          .compress(qx.lang.Json.stringify(data[2]))
          .compress(qx.lang.Json.stringify(data[3]))
          .run();
      }).bind(this);

      attempt(function()
      {
        count += 1;
        if(count > 8)
        {
          this.resume(Function.lambda());
        }
        else
        {
          attempt(arguments.callee);
        }
      });
      
      this.wait();
    },
    
    testCompressFail : function()
    {
      this.testee.addListener("fail", function()
      {
        this.resume(Function.lambda());
      }, this);
        
      this.testee
        .compress([1, 2, 3])
        .run();
      
      this.wait();
    },
    
    testDecompress : function()
    {
      var data = [
        "DcoxDgIhEADArxBqNoHd5WCfYGFj7IzFwoGSXE4LjS/xXX5JuinmEigKeomMzh5e91HVYDK/rxHr7PFx++iYOLfd" +
          "nPQ51uleIy6aGXwOBZglQImSgatQq4GL73G22hp1WQJwkAScqYCSrpAIGVVSK6zW7e9tu/4B",
        "Fcw5DgIxDEDRu6QeS3ZsZynhCpSIwhkngMQiRXN/EbpfPP0rsVbiGCNu4TSP57D9CFs42+v9nY9uqy/HtM+9z5VE" +
          "I8fGHShWA/GcwZgLeCrN14Tc658p5sapQE8JQXZSsFEFeGhC1kQoupgkc/LKgFIjiA6H2qTAzkVdsaWIFG4/",
        "DcoxDsMgDEbhuzDHEgYbw5wjdKw6/MFQZUmGVj1/md4bvidnbRxbLWkLO64fPmELj3tcK/v9vs7v+RvrJcJ1QEg8" +
          "gsSKEiYiaR+tJ3iXzosZJE2LTtYjkxRPdHitZLm2YiiZZ1xsSoOYOiXWSTJLJigr5aNn4EhaTcLrDw==",
        "JYxNC8IwEET/Ssi5C0mz+bp7EsVLb+Ih2W40hVao1N/vgseZ92bu1vmUffbWDXp6sTrtXNa+PfWgz8f+Xvqm/+DS" +
          "v1KrK5fPsbO6NTX1lQXK3phACWqoI2AwBKWMBJVmTAmzM7GJJpGRGkJq1gGy91BTiECZmUuN0Vgvmgs2UCwMro" +
          "wRECmAfGQwzDO2bKmx0Y8f"
      ];
      
      var count   = 0;
      var attempt = (function(callback)
      {
        var expectedComplete = lastfmbackup.test.mock.RecentTracksModel.data
          .slice(0, 5)
          .map(qx.lang.Json.stringify);
        this.testee.addListener("complete", function(event)
        {
          event.getData().forEach(function(item)
          {
            this.assertEquals("decompress", item.method);
            this.assertEquals(expectedComplete.shift(), item.data);
          }, this);
          qx.event.Registration.removeAllListeners(this.testee);
          callback.call(this);
        }, this);
        
        var expectedProgress = Array.range(5).map(function(i)
        {
          return i / 4;
        });
        this.testee.addListener("changeProgress", function(event)
        {
          this.assertEquals(expectedProgress.shift(), event.getData());
        }, this);
        
        this.testee
          .decompress(data[0])
          .decompress(data[1])
          .decompress(data[2])
          .decompress(data[3])
          .run();
      }).bind(this);

      attempt(function()
      {
        count += 1;
        if(count > 8)
        {
          this.resume(Function.lambda());
        }
        else
        {
          attempt(arguments.callee);
        }
      });
      
      this.wait();
    },
    
    testDecompressFail : function()
    {
      this.testee.addListener("fail", function()
      {
        this.resume(Function.lambda());
      }, this);
        
      this.testee
        .decompress([1, 2, 3])
        .run();
      
      this.wait();
    }
    
  }
    
});